define([
    'jquery',
    'underscore',
    'backbone',
    'validator',
    'text!template/backyard/manage/parameter_personnel_create.html',
    'collection/backyard/manage/parameter_personnel_list',
    'view/backyard/manage/parameter_personnel_list',
    'view/backyard/manage/parameter_error'
], function(
        $,
        _,
        Backbone,
        validator,
        CreateTemplate,
        PersonnelCollection,
        PersonnelListView,
        ParameterError
    ){

    return Backbone.View.extend({
        el: '[bb-place-personnel]',
        template: _.template(CreateTemplate),

        events: {
            'click [bb-remove]': 'remove',
            'click [bb-create]': 'create',
            'click [bb-create-submit]': 'submit',
            'click [bb-create-cancel]': 'cancel'
        },

        initialize: function(options) {
            var _this = this;
            _this.options = options;

            new PersonnelCollection();
        },

        remove: function(e) {
            var _this = this;
            var identification = $(e.target).attr('data-id');

            $.ajax({
                url: '/api/backyard/manage/parameter/personnel',
                type: 'delete',
                dataType: 'json',
                data: JSON.stringify({ id: identification}),
                success: function(msg) {
                    if (msg == 'success') {
                        _this.rerender();
                    } else {
                        new ParameterError({
                            type: 'warning',
                            content: '删除失败'
                        });
                        _this.dismiss();
                    }
                }
            });
        },

        create: function() {
            var _this = this;

            $.ajax({
                url: '/api/backyard/manage/parameter/position',
                type: 'get',
                dataType: 'json',
                success: function(positions) {
                    _this.$el.find('tfoot tr').hide();
                    _this.$el.find('tfoot').append(_this.template({positions: positions}));
                }
            });
        },

        submit: function(e) {
            var _this = this;
            e.preventDefault();

            var workno = this.$el.find('[name="workno"]').val();
            var name = this.$el.find('[name="name"]').val();
            var domain = this.$el.find('[name="domain"]').val();
            var position = this.$el.find('[name="position"]').val();

            if (
                validator.isNull(workno) &&
                validator.isNull(name) &&
                validator.isNull(domain) &&
                validator.isNull(position)
            ) {
                // blank validation
                new ParameterError({
                    type: 'warning',
                    content: '添加内容不能为空'
                });
                _this.dismiss();
            } else {
                // submit
                $.ajax({
                    url: '/api/backyard/manage/parameter/personnel',
                    type: "post",
                    dataType: 'json',
                    data: JSON.stringify({
                        workno: workno,
                        name: name,
                        domain: domain,
                        position: position
                    }),
                    success: function(msg) {
                        if (msg == 'duplicate') {
                            // duplicate validation
                            new ParameterError({
                                type: 'warning',
                                content: '该项目已存在'
                            });
                            _this.dismiss();
                        } else if (msg == 'fail') {
                            new ParameterError({
                                type: 'danger',
                                content: '添加出错，请稍候再试'
                            });
                            _this.dismiss();
                        } else {
                            _this.rerender();
                        }
                    }
                });
            }
        },

        cancel: function() {
            this.$el.find('[bb-create-cancel]').parents('tr').remove();
            this.$el.find('tfoot tr').show();
        },

        rerender: function() {
            var _this = this;
            new PersonnelListView(); // refresh the view
            _this.cancel(); // change the table foot to normal and empty the input
        },

        dismiss: function() {
            // Dismiss the alert message
            this.$el.find('input').each(function() {
                this.click(function() {
                    $('.alert').remove();
                });
            });
        }
    });

});
